import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: tgy
 * @Date: 2020-07-28 11:44
 */

class Client{

    private Lock lock = new ReentrantLock();

    private Condition con01 = lock.newCondition();
    private Condition con02 = lock.newCondition();
    private Condition con03 = lock.newCondition();
    private volatile int index;


    public void run01(){

//        new ThreadPoolExecutor()
        lock.lock();

        try {

            while (index != 0){

                con01.await();
            }
            System.out.println();
            System.out.println("================");
            System.out.println("run01 运行");
            index = 1;
            con02.signal();
        }catch (Exception e){


        }finally {
            lock.unlock();
        }

    }

    public void run02(){

        lock.lock();

        try {

            while (index != 1){

                con02.await();
            }

            System.out.println("run02 运行");
            index = 2;
            con03.signal();
        }catch (Exception e){


        }finally {
            lock.unlock();
        }

    }

    public void run03(){

        lock.lock();

        try {

            while (index != 2){

                con03.await();
            }

            System.out.println("run03 运行");
            index = 0;
            con01.signal();
        }catch (Exception e){


        }finally {
            lock.unlock();
        }

    }

}

public class 指定线程运行 {

    public static void main(String[] args) {



        Client client = new Client();

        for (int i = 0; i < 10; i++) {

            new Thread(()->{

                client.run01();
            }).start();

            new Thread(()->{

                client.run02();
            }).start();

            new Thread(()->{

                client.run03();
            }).start();
        }

    }
}
